package com.kr.redeye.function;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONSerializer;

import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kr.redeye.dto.FunctionTableDto;
import com.kr.redeye.util.HttpResponse;



/**
 * 기능 컨트롤러
 * <pre>
 * 
 * </pre>
 *
 * <pre>
 * <b>History:</b>
 *    강수, 1.0, 2013.9.04 초기작성
 * </pre>
 *
 * @author 최종 수정자
 * @version 1.0, 2013.9.04 메서드 추가함
 * @see    None
 */
@Controller
public class FunctionController {
	
	private HttpSession loginSession = null;
	private static final String DEFAULT_REQUEST_MAPPING = "/function.do";
	private static final Logger logger = LoggerFactory.getLogger(FunctionController.class);
	
	/** service */
	@Resource(name = "FunctionService")
	private FunctionService FunctionService;
	

	/**
	 * jsp페이지 호출
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @return ModelAndView, jsp로 페이지 포워드 및 오브젝트 설정
	 * @exception 
	 */
	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=leaderAddForm"})
	public ModelAndView leaderAddForm(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("param") FunctionTableDto param) {
		logger.info("action=leaderAddForm "+ param.toString(ToStringStyle.MULTI_LINE_STYLE));
		ModelAndView mv = new ModelAndView();
		
		loginSession = request.getSession();
		String userId = (String) loginSession.getAttribute("userId");
		String project_idx = (String) loginSession.getAttribute("project_idx");
		String isLeader = (String) loginSession.getAttribute("isLeader");
//		mv.addObject("actionMode", param.getActionMode());
		mv.addObject("project_idx", project_idx);
		mv.addObject("userId", userId);
		mv.addObject("isLeader", isLeader);
		
		mv.setViewName("function/func_leaderadd");
		return mv;
	}
	
	/**
	 * db리절트셋 json반환
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @param FunctionTableDto param , 기능 테이블 dto
	 * @return json object, json반환
	 * @exception 
	 */
	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=getList"})
	public void getList(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("param") FunctionTableDto param) {
		logger.info("action=getIwdmList "+param.toString(ToStringStyle.MULTI_LINE_STYLE));
		List<?> resultSets = FunctionService.getList(param);
		HttpResponse.sendMessage(response, JSONSerializer.toJSON(resultSets).toString());
	}
	
	/**
	 * 기능데이터 추가
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @param FunctionTableDto param , 기능 테이블 dto
	 * @return int succ, 추가된 db row개수 반환
	 * @exception 
	 */
	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=insert"})
	public void insert(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("param") FunctionTableDto param) {
		logger.info("action=insert "+ param.toString(ToStringStyle.MULTI_LINE_STYLE));
		int succ = 0;
		succ = FunctionService.insert(param);
		HttpResponse.sendMessage(response, Integer.toString(succ));

	}
	
	/**
	 * 기능데이터 수정
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @param FunctionTableDto param , 기능 테이블 dto
	 * @return int succ, 업데이트된 db row개수 반환
	 * @exception 
	 */
	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=upd"})
	public void upd(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("param") FunctionTableDto param) {
		logger.info("action=upd "+ param.toString(ToStringStyle.MULTI_LINE_STYLE));
		int succ = 0;
		succ = FunctionService.upd(param);
		HttpResponse.sendMessage(response, Integer.toString(succ));

	}
	
	/**
	 * 기능데이터 삭제
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @param FunctionTableDto param , 기능 테이블 dto
	 * @return int succ, 삭제된 db row개수 반환
	 * @exception 
	 */
	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=del"})
	public void del(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("param") FunctionTableDto param) {
		logger.info("action=del "+ param.toString(ToStringStyle.MULTI_LINE_STYLE));
		int succ = 0;
		succ = FunctionService.del(param);
		HttpResponse.sendMessage(response, Integer.toString(succ));

	}
	
	
}
